import {useLocaleContext} from "@arcblock/ux/lib/Locale/context";
import moment from 'moment';

const timeAgo = (date) => {

    const {t} = useLocaleContext();

    const pastDate = new Date(date);
    const now = new Date();
    const secondsPast = Math.floor((now - pastDate) / 1000);

    if (secondsPast < 60) {
        return secondsPast + t('time.seconds');
    } else if (secondsPast < 3600) {
        const minutes = Math.floor(secondsPast / 60);
        return minutes + t('time.minutes');
    } else if (secondsPast < 86400) {
        const hours = Math.floor(secondsPast / 3600);
        return hours + t('time.hours');
    } else if (secondsPast < 2592000) {
        const days = Math.floor(secondsPast / 86400);
        return days + t('time.days');
    } else if (secondsPast < 31536000) {
        const months = Math.floor(secondsPast / 2592000);
        return months + t('time.months');
    } else {
        return moment(pastDate).format('YYYY-MM-DD HH:mm:ss');
    }
}

export default timeAgo;
